﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;

namespace ReportTracking.Services
{
    /// <summary>
    /// Summary description for DefinedSpeed
    /// </summary>
    public class DefinedSpeed : IHttpHandler
    {
        string sPath = "";
        public void ProcessRequest(HttpContext context)
        {
            
            sPath = context.Request.PhysicalApplicationPath + "XMLData/speed.xml";
            ReportTracking.DefinedSpeed dfsp = new ReportTracking.DefinedSpeed(sPath);
            context.Response.ContentType = "text/plain";
            string q = context.Request.QueryString["q"];
            DateTime fd = Convert.ToDateTime(context.Request.QueryString["fd"]);
            DateTime td = Convert.ToDateTime(context.Request.QueryString["td"]);
            int page = Convert.ToInt32(context.Request.QueryString["page"]);
            int nRows = Convert.ToInt32(context.Request.QueryString["rows"]);
            int nTotal = 0;
            int nPage = 0;
           
                string oper = context.Request.Form["oper"];

                if (oper == "add")
                {
                    string sLPT = context.Request.Form["LPT"];
                    string sTENPT = context.Request.Form["TENPT"];
                    double dVANTOC = Convert.ToDouble(context.Request.Form["VANTOC"]);
                    dfsp.InsertXML(sLPT, sTENPT, (float)dVANTOC);
                }
                if (oper == "edit")
                {
                    string sLPT = context.Request.Form["LPT"];
                    string sTENPT = context.Request.Form["TENPT"];
                    double dVANTOC = Convert.ToDouble(context.Request.Form["VANTOC"]);
                    dfsp.UpdateRecord(sLPT, sTENPT, (float)dVANTOC);
                }
                if (oper=="del")
                {
                    string idLPT = HttpContext.Current.Request.Cookies["idLPT"].Value;
                    dfsp.DeleteRecord(idLPT);
                }
                

            StringBuilder sData = new StringBuilder();
            string sTable = "";

            dfsp.CreateFileXmlSpeed();

            sTable = Newtonsoft.Json.JsonConvert.SerializeObject(GetData(ref nTotal, ref nPage, nRows, fd, td));
            sData.Append("{");
            sData.Append("page:");
            sData.Append(page);
            sData.Append(",");
            sData.Append("total:");
            sData.Append(nPage);
            sData.Append(",");
            sData.Append("records:");
            sData.Append(nTotal);
            sData.Append(",");
            sData.Append("rows:");
            sData.Append(sTable);
            sData.Append("}");
            context.Response.Write(Newtonsoft.Json.JsonConvert.DeserializeObject(sData.ToString()));
            

        }
        private DataTable GetData(ref int total, ref int page, int rows, DateTime fd, DateTime td)
        {
            DataTable tb = CreateDatatable();
            try
            {
                ReportTracking.DefinedSpeed dfsp = new ReportTracking.DefinedSpeed(sPath);
                if (dfsp.LoadXML().Tables.Count ==0)
                {
                   
                    ReportTracking.ReportTrackingDTT.TrackingReportClient rptrk = new ReportTracking.ReportTrackingDTT.TrackingReportClient();
                    DataSet ds = rptrk.GetBusinessUnitType(fd, td);
                    if (ds.Tables.Count >0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            dfsp.InsertXML(ds.Tables[0].Rows[i]["BusinessUnitType"].ToString(), ds.Tables[0].Rows[i]["BusinessUnitType"].ToString(), 0);
                        }
                    }
                }
                tb = dfsp.LoadXML().Tables[0];
                total = tb.Rows.Count;    
                page = Utilities.PageNumber(total,rows);
                 
            }
            catch (System.Exception ex)
            {
                page = 0;
                total = 0;
            }
            return tb;
            
        }
        private static DataTable CreateDatatable()
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("LX", typeof(string));
            tb.Columns.Add("Speed", typeof(int));
            tb.Dispose();
            return tb;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}